package com.xiehao.springbootmybaits;

import com.xiehao.springbootmybaits.entity.Book;
import org.apache.ibatis.jdbc.SQL;

public class SqlContext {
    //动态查询
    public String searchBookSql(Book book){
        return new SQL(){
            {
                SELECT("*");
                FROM("book");
                if (book.getName() != null && book.getName() != "") {
                    WHERE("name like '%'#{name}'%'");
                }
                if (book.getCategory() != null && book.getCategory() != "") {
                    WHERE("category=#{category}");
                }
                if (book.getAuthor() != null && book.getAuthor() != "") {
                    WHERE("author=#{author}");
                }
            }
        }.toString();
    }
}
